/*
 * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
 * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
 * CONSEQUENTLY, CMOSTEK SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
 * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 *
 * Copyright (C) CMOSTEK SZ.
 */

/*!
 * @file    cmt2300a.h
 * @brief   CMT2300A transceiver RF chip driver
 *
 * @version 1.3
 * @date    Jul 17 2017
 * @author  CMOSTEK R@D
 */

#ifndef __CMT2300A_H
#define __CMT2300A_H

#include "nonos.h"
#include "cmtspi3.h"
#include "cmt2300a_defs.h"

#ifdef __cplusplus 
extern "C" { 
#endif


/**
 * HAL 接口
 */
extern driver_cmtspi3_t *cmtspi;

#define CMT2300A_ReadReg(addr) 			cmtspi3_read_reg(cmtspi, addr)
#define CMT2300A_WriteReg(addr, ch) 	cmtspi3_write_reg(cmtspi, addr, ch)
#define CMT2300A_ReadFifo(buf, len)		cmtspi3_read_fifo(cmtspi, buf, len)
#define CMT2300A_WriteFifo(buf, len)	cmtspi3_write_fifo(cmtspi, buf, len)
#define CMT2300A_DelayUs(x)				osDelayUs(x)
#define CMT2300A_DelayMs(x)				osDelayMs(x)
#define CMT2300A_ReadGpio1()			(cmtspi->gpio())
#define CMT2300A_ReadGpio2()			(cmtspi->gpio())


/* RF state machine */
typedef enum {
    RF_STATE_IDLE = 0,
    RF_STATE_RX_START,
    RF_STATE_RX_WAIT,
    RF_STATE_RX_DONE,
    RF_STATE_RX_TIMEOUT,
    RF_STATE_TX_START,
    RF_STATE_TX_WAIT,
    RF_STATE_TX_DONE,
    RF_STATE_TX_TIMEOUT,
    RF_STATE_ERROR,
} EnumRFStatus;

/* RF process function results */
typedef enum {
    RF_IDLE = 0,
    RF_BUSY,
    RF_RX_DONE,
    RF_RX_TIMEOUT,
    RF_TX_DONE,
    RF_TX_TIMEOUT,
    RF_ERROR,
} EnumRFResult;

typedef struct {
	// GPIO 驱动函数
	FUNC_GPIO_Out csb;
	FUNC_GPIO_Out fcsb;
	FUNC_GPIO_Out sclk;
	FUNC_GPIO_Out dout;
	FUNC_GPIO_ReadIn din;
	FUNC_GPIO_ReadIn gpio;
} driver_cmt2300a_t;

/* Enable the auto switch chip status */
#define ENABLE_AUTO_SWITCH_CHIP_STATUS   

/* ************************************************************************
   The following are for chip status controls.
*  ************************************************************************ */
bool CMT2300A_AutoSwitchStatus(uint8_t nGoCmd);
inline static bool CMT2300A_GoSleep(void)		{ return CMT2300A_AutoSwitchStatus(CMT2300A_GO_SLEEP); }
inline static bool CMT2300A_GoStby(void) 		{ return CMT2300A_AutoSwitchStatus(CMT2300A_GO_STBY); }
inline static bool CMT2300A_GoTFS(void) 		{ return CMT2300A_AutoSwitchStatus(CMT2300A_GO_TFS); }
inline static bool CMT2300A_GoRFS(void) 		{ return CMT2300A_AutoSwitchStatus(CMT2300A_GO_RFS); }
inline static bool CMT2300A_GoTx(void) 			{ return CMT2300A_AutoSwitchStatus(CMT2300A_GO_TX); }
inline static bool CMT2300A_GoRx(void) 			{ return CMT2300A_AutoSwitchStatus(CMT2300A_GO_RX); }

inline static void CMT2300A_SoftReset(void) 	{ CMT2300A_WriteReg(0x7F, 0xFF); }

/*! ********************************************************
* @name    CMT2300A_GetChipStatus
* @desc    Get the chip status.
* @return
*          CMT2300A_STA_PUP
*          CMT2300A_STA_SLEEP
*          CMT2300A_STA_STBY
*          CMT2300A_STA_RFS
*          CMT2300A_STA_TFS
*          CMT2300A_STA_RX
*          CMT2300A_STA_TX
*          CMT2300A_STA_EEPROM
*          CMT2300A_STA_ERROR
*          CMT2300A_STA_CAL
* *********************************************************/
inline static uint8_t CMT2300A_GetChipStatus(void) 	{ return CMT2300A_ReadReg(CMT2300A_CUS_MODE_STA) & CMT2300A_MASK_CHIP_MODE_STA; }


/* ************************************************************************
*  The following are for chip interrupts, GPIO, FIFO operations.
*  ************************************************************************ */
void CMT2300A_ConfigGpio(uint8_t nGpioSel);
void CMT2300A_ConfigInterrupt(uint8_t nInt1Sel, uint8_t nInt2Sel);
void CMT2300A_SetInterruptPolar(bool bActiveHigh);
void CMT2300A_SetFifoThreshold(uint8_t nFifoThreshold);
void CMT2300A_EnableAntennaSwitch(uint8_t nMode);
void CMT2300A_EnableInterrupt(uint8_t nEnable);
void CMT2300A_EnableRxFifoAutoClear(bool bEnable);
void CMT2300A_EnableFifoMerge(bool bEnable);
void CMT2300A_EnableReadFifo(void);
void CMT2300A_EnableWriteFifo(void);
void CMT2300A_RestoreFifo(void);
uint8_t CMT2300A_ClearTxFifo(void);
uint8_t CMT2300A_ClearRxFifo(void);
uint8_t CMT2300A_ClearInterruptFlags(void);


/* ************************************************************************
*  The following are for Tx DIN operations in direct mode.
*  ************************************************************************ */
void CMT2300A_ConfigTxDin(uint8_t nDinSel);
void CMT2300A_EnableTxDin(bool bEnable);
void CMT2300A_EnableTxDinInvert(bool bEnable);


/* ************************************************************************
*  The following are general operations.
*  ************************************************************************ */
bool CMT2300A_IsExist(void);
uint8_t CMT2300A_GetRssiCode(void);
int CMT2300A_GetRssiDBm(void);
void CMT2300A_SetFrequencyChannel(uint8_t nChann);
void CMT2300A_SetFrequencyStep(uint8_t nOffset);
void CMT2300A_SetPayloadLength(uint16_t nLength);
void CMT2300A_EnableLfosc(bool bEnable);
void CMT2300A_EnableLfoscOutput(bool bEnable);
void CMT2300A_EnableAfc(bool bEnable);
void CMT2300A_SetAfcOvfTh(uint8_t afcOvfTh);


/* ************************************************************************
*  The following are for chip initializes.
*  ************************************************************************ */
void CMT2300A_Init(void);
bool CMT2300A_ConfigRegBank(uint8_t base_addr, const uint8_t bank[], uint8_t len);

//#define ENABLE_ANTENNA_SWITCH

// 接收数据处理回调函数
extern std_err_t CMT2300A_rxHandler(uint8_t buf[], uint16_t len, int rssi);

void CMT2300A_GpioIrqHandler(void);
void CMT2300A_StartRx(void);
std_err_t CMT2300A_syncTx(uint8_t buf[], uint16_t len, uint32_t timeout);


#ifdef __cplusplus
} 
#endif

#endif
